package com.woan.api.servlet;
import java.io.IOException;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.woan.api.services.UserClientServices;
import com.woan.api.util.HttpTool;
import com.woan.api.vo.Json;

import net.sf.json.JSONObject;

public class Auth extends HttpServlet {
	private static final long serialVersionUID = 1L;
	HttpTool ht = new HttpTool();
	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException {
		Hashtable<String, String> hash = null;
		hash = this.ht.getRequestBody(request);
		System.out.println("视频服务器发送的内容---------"+hash);
		auth(request, response,hash);	
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void init() throws ServletException {
	}

	public void auth(HttpServletRequest request, HttpServletResponse response,Hashtable<String, String> hash)
			throws ServletException, IOException {
		String msg = "1 -1";
		String username = (String) hash.get("username");
		if ((username == null) || (username.equals(""))) {
			username = (String) hash.get("userName");
		}
		System.out.println("登录名---------"+username);
		String pwd = (String) hash.get("pwd");
		String service_code = (String) hash.get("service_code");
		String rname = "";
		String uname = "";
		String devicenum = "";
		// 由于视频服务器需要修改的关系，这里采用拼接的方式，将需要的参数传给服务器，然后再次转发回来
		if (username.contains("|")) 
		{
			String para[] = username.split("\\|");
			rname = para[0];
			System.out.println("登录名---------"+rname);
			uname = para[1];
			System.out.println("登录名---------"+uname);
			devicenum = para[2];
			System.out.println("登录名---------"+devicenum);
		}
		ServletContext servletContext = this.getServletContext();     
        WebApplicationContext wac = null;     
        wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);    
        UserClientServices userClientServices = (UserClientServices) wac.getBean("userClientServices");
        Json objson = new Json();
        objson = userClientServices.loginInfo(rname, uname,pwd,devicenum);
        System.out.println("登录消息返回值---------"+objson.getResults());
		if ("0".equals(objson.getResults()))
		{
			msg = "0 " + username;
		}
		else 
		{
			msg = "1 -1";
		}
		System.out.println("登录消息---------"+msg);
		this.ht.retData(response, msg);
	}
}